﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormNhomTaiSan : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        public FormNhomTaiSan()
        {
            InitializeComponent();
        }
#region Event
        private void FormNhomTaiSan_Load_1(object sender, EventArgs e)
        {
            Loaddata();
        }
        private void BttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormNhomTaiSanThem();
            f.ShowDialog();
        }

        private void BttXoa_Click(object sender, EventArgs e)
        {
            if (idtam != 0)
            {
                var d = db.NhomTaiSans.Find(idtam);
                if (MessageBox.Show("Bạn muốn xóa nhóm: " + d.TenNhomTaiSan + "!", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Delete(idtam);
                }
            }
            else
            {
                MessageBox.Show("Không có dữ liệu xóa!");
            }
        }

        private void dgvNhomTaiSan_RowHeaderCellChanged(object sender, DataGridViewRowEventArgs e)
        {
            int rowindex = e.Row.Index;
            DataGridViewRow row = dgvNhomTaiSan.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }

        private void dgvNhomTaiSan_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvNhomTaiSan.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }
        private void BttCapNhap_Click(object sender, EventArgs e)
        {
            if (idtam != 0)
            {
                var f = new FormNhomTaiSanCapNhap();
                f.id = idtam;
                f.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn tài sản cập nhập!");
            }
        }

        private void dgvNhomTaiSan_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvNhomTaiSan.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            if (idtam != 0)
            {
                var f = new FormNhomTaiSanCapNhap();
                f.id = idtam;
                f.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn tài sản cập nhập!");
            }
        }
        private void dgvNhomTaiSan_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void BttThoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        private void BttLamMoi_Click(object sender, EventArgs e)
        {
            Loaddata();
        }
#endregion Event
#region Metho

        public void Loaddata()
        {
            dgvNhomTaiSan.DataSource = db.NhomTaiSans.ToList();
            reset();
        }
        

        private void Delete(int id = 0)
        {
            NhomTaiSan nts = db.NhomTaiSans.Find(id);
            db.NhomTaiSans.Remove(nts);
            db.SaveChanges();
            Loaddata();
        }

        void reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }

        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
#endregion Metho

       
        
    }
}
